﻿using BCCommon;
using BCCommon.Enums;
using BCData.CRM.IndustryCategory;
using BCDto.CRM.IndustryCategory;
using BCEntity.CRM.IndustryCategory;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.CRM.IndustryCategory
{
    public class IndustryCategoryService : IIndustryCategoryService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IIndustryCategoryData industryCategoryData;
        private readonly string key = string.Format("{0}", EntityNameConst.IndustryCategoryEntity);

        public IndustryCategoryService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IIndustryCategoryData industryCategoryData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.industryCategoryData = industryCategoryData;
        }

        #region 检查行业类别是否存在
        public bool Exists(long industryCategoryId)
        {
            return this.industryCategoryData.Exists(industryCategoryId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加行业类别
        public IndustryCategoryDto Add(IndustryCategoryRequestDto requestDto)
        {
            var entity = requestDto.As<IndustryCategoryEntity>();

            var result = this.industryCategoryData.Add(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<IndustryCategoryDto>();
        }
        #endregion

        #region 修改行业类别
        public IndustryCategoryDto Update(IndustryCategoryPutDto putDto)
        {
            if (!this.Exists(putDto.IndustryCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<IndustryCategoryEntity>();
            var result = this.industryCategoryData.Update(entity).GetAwaiter().GetResult().As<IndustryCategoryDto>();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 删除行业类别
        public bool Delete(long industryCategoryId)
        {
            if (!this.Exists(industryCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.industryCategoryData.Delete(industryCategoryId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取行业类别
        public IndustryCategoryDto Get(long industryCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, industryCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.industryCategoryData.GetEntity(industryCategoryId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<IndustryCategoryDto>();
        }
        #endregion

        #region 获取行业类别列表
        public IEnumerable<IndustryCategoryDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                return this.industryCategoryData.GetList().GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<IndustryCategoryDto>>();
        }
        #endregion

        #region 获取行业类别分页
        public PageableList<IndustryCategoryDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.industryCategoryData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<IndustryCategoryDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<IndustryCategoryDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}